package cn.javaeye.lonlysky.lforum.service;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.hibernate.SimpleHibernateTemplate;

import cn.javaeye.lonlysky.lforum.entity.web.Info;

@Service
@Transactional
public class InfoManager {

	private SimpleHibernateTemplate<Info, Integer> infoDAO;
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		infoDAO = new SimpleHibernateTemplate<Info, Integer>(sessionFactory, Info.class);
	}

	public void save(Info info) {
			infoDAO.save(info);
	}

	public Info get(Integer id) {
		return infoDAO.get(id);
	}
	public void remove(Info info) {
		infoDAO.delete(info);
	}
	public void remove(Integer id) {
		infoDAO.delete(id);
	}
	/**
	 * 
	 * @return
	 */
	public List<Info> findAll() {
		return infoDAO.findAll();
	}
	/**
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Info> findAllByUser(Integer userid) {
		return infoDAO.find("from Info where user.id=?", userid);
	}
	public List<Info> findAllParent(Integer pid) {
		return infoDAO.findByProperty("parentType.id", pid);
	}

	@SuppressWarnings("unchecked")
	public List<Info> findAllFarther(Integer fid) {
		return infoDAO.find("from Info info where info.parentType.id in (select pt.id from ParentType pt where  pt.fartherType.id=? )", fid);
	}

	@SuppressWarnings("unchecked")
	public List<Info> findByFarther(Integer fid) {
		return infoDAO.createQuery("from Info info where info.parentType.id in (select pt.id from ParentType pt where  pt.fartherType.id=? )", fid).setMaxResults(10).list();
	}

	@SuppressWarnings("unchecked")
	public List<Info> findParent(Integer fid) {
		return infoDAO.find("from Info info where info.parentType.id in (select pt.id from ParentType pt where  pt.fartherType.id=? )", fid);
	}

	@SuppressWarnings("unchecked")
	public List<Info> findInfoParther(Integer fid) {
		return infoDAO.createQuery("from Info info where info.parentType.id =?", fid).setMaxResults(10).list();
	}

	public SimpleHibernateTemplate<Info, Integer> getInfoDAO() {
		return infoDAO;
	}

	public void setInfoDAO(SimpleHibernateTemplate<Info, Integer> infoDAO) {
		this.infoDAO = infoDAO;
	}
	
	
}
